package com.party.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.party.common.base.ApiResult;
import com.party.common.entity.MyPage;
import com.party.entity.PbBanner;
import com.party.entity.PbDevice;
import com.party.entity.PbStationDevice;
import com.party.entity.PbUser;
import com.party.entity.dto.QueryBannerDto;
import com.party.entity.vo.BannerVo;
import com.party.entity.vo.ZikongEntity;
import com.party.service.PbBannerService;
import com.party.service.PbDeviceService;
import com.party.service.PbStationDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * banner
 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2024-10-25
 */
@Slf4j
@RestController
@RequestMapping("/basicInfo")
@Api(tags = "banner相关api接口只针对大屏展示", value = "banner相关api接口只针对大屏展示")
public class PbBasicController {
    @Resource
    private PbBannerService pbBannerService;
    @Resource
    private PbDeviceService pbDeviceService;
    @Resource
    private PbStationDeviceService pbStationDeviceService;
    @ApiOperation("获取banner列表")
    @GetMapping("/getBanner")
    public ApiResult<List<BannerVo>> getBanner(
            @ApiParam("【banner投放位置，不传默认返回所有】" +
                    " 1 党建书城轮播图 2 红色回响轮播图 3 党建风采轮播图  4.党建风采活动图  5.自控电台轮播图")
            @RequestParam(value = "position", required = false) Integer position, @RequestParam(value = "stationCode", required = false) String stationCode) {
        log.info("请求设备参数"+stationCode, LocalDateTime.now());
        if(position==1)
        {
            //党建书城
            LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
            query.eq(PbBanner::getType, 5);
            List<PbBanner> list = pbBannerService.list(query);
            List<BannerVo> bannerVoList=new ArrayList<>();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    BannerVo bannerVo=new BannerVo();
                    bannerVo.setUrl(list.get(i).getCover());
                    bannerVo.setTarget(list.get(i).getTarget());
                    bannerVoList.add(bannerVo);
                }
            }
            return   ApiResult.ok1(bannerVoList);
        }
        if(position==2)
        {
            //红色回响
            LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
            query.eq(PbBanner::getType, 6);
            List<PbBanner> list = pbBannerService.list(query);
            List<BannerVo> bannerVoList=new ArrayList<>();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    BannerVo bannerVo=new BannerVo();
                    bannerVo.setUrl(list.get(i).getCover());
                    bannerVo.setTarget(list.get(i).getTarget());
                    bannerVoList.add(bannerVo);
                }
            }
            return   ApiResult.ok1(bannerVoList);
        }
        if(position==3)
        {
            //党建风采
            LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
            query1.eq(PbDevice::getDeviceCode,stationCode);
            PbDevice one = pbDeviceService.getOne(query1);
            Long id = one.getId();
            LambdaQueryWrapper<PbStationDevice> query2 = new LambdaQueryWrapper<>();
            query2.eq(PbStationDevice::getDeviceId,id);
            query2.orderByDesc(PbStationDevice::getCreateTime);
            List<PbStationDevice> list1 = pbStationDeviceService.list(query2);
            List<PbBanner> list = new ArrayList<>();
            if (list1 != null) {
                for (int i = 0; i < list1.size(); i++) {
                    LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
                    query.eq(PbBanner::getType, 2);
                    query.eq(PbBanner::getStatus, 1);
                    query.eq(PbBanner::getId, list1.get(i).getStationId());
                    if (pbBannerService.getOne(query) != null) {
                        list.add(pbBannerService.getOne(query));
                    }
                }
            }
            List<BannerVo> bannerVoList=new ArrayList<>();
            if (list != null&&list.size()>0) {
                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getCover().contains(","))
                    {
                        String[] split = list.get(i).getCover().split(",");
                        for (int j = 0; j < split.length; j++) {
                            BannerVo bannerVo=new BannerVo();
                            bannerVo.setUrl(split[j]);
                            bannerVo.setTarget(list.get(i).getTarget());
                            bannerVo.setName(list.get(i).getTitle());
                            bannerVo.setDangjianDescribe(list.get(i).getContent());
                            bannerVoList.add(bannerVo);
                        }

                    }else{
                        BannerVo bannerVo=new BannerVo();
                        bannerVo.setUrl(list.get(i).getCover());
                        bannerVo.setTarget(list.get(i).getTarget());
                        bannerVo.setName(list.get(i).getTitle());
                        bannerVo.setDangjianDescribe(list.get(i).getContent());
                        bannerVoList.add(bannerVo);
                    }

                }
            }
            else{
                BannerVo bannerVo=new BannerVo();
                bannerVo.setUrl("https://party-build.obs.cn-north-4.myhuaweicloud.com:443/%E4%B8%BB%E5%9B%BE+%281%29.png");
                bannerVo.setTarget("");
                bannerVo.setName("党建风采展示示意图");
                bannerVo.setDangjianDescribe("  机构党建风采展示阵地，方便用户通过微信小程序“AI自控助手”或PC端“党建工作AI助手后台管理系统”一键发布相关党建活动，服务于党政机关、高校、企业等通过图文实录的方式搭建个性化党建展示和学习的空间，实现月度主题党日活动自动记录和展示，做到工作留痕，助力党建工作科学化、信息化发展。");
                bannerVoList.add(bannerVo);
            }
            return   ApiResult.ok1(bannerVoList);
        }
        if(position==4)
        {
            //主题日历
            LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
            query1.eq(PbDevice::getDeviceCode,stationCode);
            PbDevice one = pbDeviceService.getOne(query1);
            Long id = one.getId();
            LambdaQueryWrapper<PbStationDevice> query2 = new LambdaQueryWrapper<>();
            query2.eq(PbStationDevice::getDeviceId,id);
            query2.orderByDesc(PbStationDevice::getCreateTime);
            List<PbStationDevice> list1 = pbStationDeviceService.list(query2);
            List<PbBanner> list = new ArrayList<>();
            if (list1 != null) {
                for (int i = 0; i < list1.size(); i++) {
                    LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
                    query.eq(PbBanner::getType, 4);
                    query.eq(PbBanner::getStatus, 1);
                    query.eq(PbBanner::getId, list1.get(i).getStationId());
                    if (pbBannerService.getOne(query) != null) {
                        list.add(pbBannerService.getOne(query));
                    }
                }
            }
            List<BannerVo> bannerVoList=new ArrayList<>();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    BannerVo bannerVo=new BannerVo();
                    if(list.get(i).getCover().contains(","))
                    {
                        String[] split = list.get(i).getCover().split(",");
                        bannerVo.setUrl(split[0]);
                    }else{
                        bannerVo.setUrl(list.get(i).getCover());
                    }

                    bannerVo.setTarget(list.get(i).getTarget());
                    String month = list.get(i).getMonth();
                    if(month.startsWith("0"))
                    {
                        String substring = month.substring(1);
                        bannerVo.setDangjianMonth(substring+"月");
                    }else{
                        bannerVo.setDangjianMonth(list.get(i).getMonth()+"月");
                    }

                    bannerVo.setName(list.get(i).getTitle());
                    bannerVoList.add(bannerVo);
                }
            }
            return   ApiResult.ok1(bannerVoList);
        }
        if(position==5)
        {
            //自控电台
            LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
            query1.eq(PbDevice::getDeviceCode,stationCode);
            PbDevice one = pbDeviceService.getOne(query1);
            Long id = one.getId();
            LambdaQueryWrapper<PbStationDevice> query2 = new LambdaQueryWrapper<>();
            query2.eq(PbStationDevice::getDeviceId,id);
            query2.orderByDesc(PbStationDevice::getCreateTime);
            List<PbStationDevice> list1 = pbStationDeviceService.list(query2);
            List<PbBanner> list = new ArrayList<>();
            if (list1 != null) {
                for (int i = 0; i < list1.size(); i++) {
                    LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
                    query.eq(PbBanner::getType, 1);
                    query.eq(PbBanner::getStatus, 1);
                    query.eq(PbBanner::getId, list1.get(i).getStationId());
                    if (pbBannerService.getOne(query) != null) {
                        list.add(pbBannerService.getOne(query));
                    }
                }
            }
            List<BannerVo> bannerVoList=new ArrayList<>();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    BannerVo bannerVo=new BannerVo();
                    bannerVo.setUrl(list.get(i).getCover());
                    bannerVo.setTarget(list.get(i).getTarget());
                    bannerVo.setName(list.get(i).getTitle());
                    bannerVo.setDangjianDescribe(list.get(i).getContent());
                    bannerVoList.add(bannerVo);
                }
            }
            return   ApiResult.ok1(bannerVoList);
        }
        if(position==6)
        {
            //轮播海报
            LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
            query1.eq(PbDevice::getDeviceCode,stationCode);
            PbDevice one = pbDeviceService.getOne(query1);
            Long id = one.getId();
            LambdaQueryWrapper<PbStationDevice> query2 = new LambdaQueryWrapper<>();
            query2.eq(PbStationDevice::getDeviceId,id);
            query2.orderByDesc(PbStationDevice::getCreateTime);
            List<PbStationDevice> list1 = pbStationDeviceService.list(query2);
            List<PbBanner> list = new ArrayList<>();
            if (list1 != null) {
                for (int i = 0; i < list1.size(); i++) {
                    LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
                    query.eq(PbBanner::getType, 3);
                    query.eq(PbBanner::getStatus, 1);
                    query.eq(PbBanner::getId, list1.get(i).getStationId());
                    if (pbBannerService.getOne(query) != null) {
                        list.add(pbBannerService.getOne(query));
                    }
                }
            }
            List<BannerVo> bannerVoList=new ArrayList<>();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    BannerVo bannerVo=new BannerVo();
                    bannerVo.setUrl(list.get(i).getCover());
                    bannerVo.setTarget(list.get(i).getTarget());
                    bannerVoList.add(bannerVo);
                }
            }
            return   ApiResult.ok1(bannerVoList);
        }
        return ApiResult.failed("暂无信息");
    }

    @ApiOperation("获取自控电台信息列表")
    @GetMapping("/getZikongList")
    public ApiResult<List<ZikongEntity>> getZikongList(@RequestParam(value = "stationCode", required = true) String stationCode) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode,stationCode);
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbStationDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbStationDevice::getDeviceId,id);
        query2.orderByDesc(PbStationDevice::getCreateTime);
        List<PbStationDevice> list1 = pbStationDeviceService.list(query2);
        List<PbBanner> list = new ArrayList<>();
        if (list1 != null&&list1.size()>0) {
            for (int i = 0; i < list1.size(); i++) {
                LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
                query.eq(PbBanner::getType, 1);
                query.eq(PbBanner::getStatus, 1);
                query.eq(PbBanner::getId, list1.get(i).getStationId());
                if (pbBannerService.getOne(query) != null) {
                    list.add(pbBannerService.getOne(query));
                }
            }
        }
        List<ZikongEntity> zikongEntityList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ZikongEntity zikongEntity = new ZikongEntity();
            zikongEntity.setName(list.get(i).getTitle());
            zikongEntity.setTitle(list.get(i).getContent());
            if(!TextUtils.isEmpty(list.get(i).getCover()))
            {
                zikongEntity.setImgUrl(list.get(i).getCover());
            }
            else{
                zikongEntity.setImgUrl("https://zhongjiaoyun.oss-cn-hangzhou.aliyuncs.com/zikong111.jpg");
            }

            zikongEntityList.add(zikongEntity);
        }

        return ApiResult.ok1(zikongEntityList);
    }
    @ApiOperation("获取轮播海报")
    @GetMapping("/getHaibao")
    public ApiResult<List<ZikongEntity>> getHaibaoList(@RequestParam(value = "stationCode", required = true) String stationCode) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode,stationCode);
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbStationDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbStationDevice::getDeviceId,id);
        query2.orderByDesc(PbStationDevice::getCreateTime);
        List<PbStationDevice> list1 = pbStationDeviceService.list(query2);
        List<PbBanner> list = new ArrayList<>();
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                LambdaQueryWrapper<PbBanner> query = new LambdaQueryWrapper<>();
                query.eq(PbBanner::getType, 3);
                query.eq(PbBanner::getStatus, 1);
                query.eq(PbBanner::getId, list1.get(i).getStationId());
                if (pbBannerService.getOne(query) != null) {
                    list.add(pbBannerService.getOne(query));
                }
            }
        }
        List<ZikongEntity> zikongEntityList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ZikongEntity zikongEntity = new ZikongEntity();
            zikongEntity.setName(list.get(i).getTitle());
            zikongEntity.setTitle(list.get(i).getContent());
            if(!TextUtils.isEmpty(list.get(i).getCover()))
            {
                zikongEntity.setImgUrl(list.get(i).getCover());
            }
            else{
                zikongEntity.setImgUrl("https://zhongjiaoyun.oss-cn-hangzhou.aliyuncs.com/zikong111.jpg");
            }

            zikongEntityList.add(zikongEntity);
        }
        return ApiResult.ok1(zikongEntityList);
    }
}

